import { defineStore } from "pinia";
import { getUserInfoFn } from "@/api/login";
import { ref } from "vue";

export const useUserStore = defineStore("user-store", () => {
  const userInfo = ref(uni.getStorageSync("userInfo") || {});
  // 是否能用ai
  const isCanUseAi = ref(false);

  function getUserInfo() {
    return new Promise(async (resolve, reject) => {
      const res = await getUserInfoFn();
      userInfo.value = res.data;
      userInfo.value.avatarUrl = res.data.avatarUrl || "https://devweb.topyunliu.com/upload/2025/04/21/file_20250421152751A001.jpeg"
      userInfo.value.isCanUseAi = userInfo.value.level ? true : false;
      uni.setStorageSync("userInfo", userInfo.value);
      resolve(userInfo.value);
    });
  };
  const updateUserInfo = (res) => {
    userInfo.value = Object.assign(userInfo.value, res);
  };

  const removeUserInfo = () => {
    uni.removeStorageSync("userInfo");
    userInfo.value = {};
  };

  return {
    userInfo,
    getUserInfo,
    removeUserInfo,
    updateUserInfo,
  };
});
